﻿//*** Guidance generated code ***//
using System;
using System.Collections.Specialized;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Http = System.Web.Http.ValueProviders;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using ProjectBase.Core;
using $prjsn$.Tools.RestApi;
using $prjsn$.RestApi;

namespace $safeprojectname$.Core.Factory
{
    [TestClass, System.Runtime.InteropServices.GuidAttribute("2387F94C-FA0A-4522-A5A8-8B64A5A44521")]
    public class ODataTest : TestBase
    {
        [TestMethod]
        public void ParseODataComplicatedString()
        {
            // given
            Http.IValueProvider provider;
            string queryString = @"http://localhost:5555/Product/api/Employee/?$Filter=Description eq Moscow AND ParentId eq 1 OR ParentId eq 2&$top=5&$Filter=BirthDate ge 2012-12-31&$Filter=Bird lt BlackBird&$orderby=Name desc&$Filter= startswith(Firm/Description,'Test')&$Filter=Description eq Saint-Petersburg&$Filter=Description ne London";

            // create engine
            var engine = new ODataParser();
            ServiceMessage message = new ServiceMessage();
            provider = engine.Parse(queryString, ref message);

            IValueContainer container = provider.ToContainer();

            Assert.IsTrue(message.Messages.Count == 0);

            Assert.IsTrue(provider.ContainsPrefix("Description"));
            Assert.IsTrue(provider.GetValue("Description").AttemptedValue.IsEqual("Moscow,Saint-Petersburg,London"));

            Assert.IsTrue(provider.ContainsPrefix("ParentId"));
            Assert.IsTrue(provider.GetValue("ParentId").AttemptedValue.IsEqual("1,2"));

            Assert.IsTrue(provider.ContainsPrefix("BirthDateFrom"));
            Assert.IsTrue(provider.GetValue("BirthDateFrom").AttemptedValue.IsEqual("2012-12-31"));

            Assert.IsTrue(provider.ContainsPrefix("BirdTo"));
            Assert.IsTrue(provider.GetValue("BirdTo").AttemptedValue.IsEqual("BlackBird"));

            Assert.IsTrue(provider.ContainsPrefix("Firm.Description"));
            Assert.IsTrue(provider.GetValue("Firm.Description").AttemptedValue.IsEqual("Test*"));

            Assert.IsTrue(provider.ContainsPrefix("TakeRows"));
            Assert.IsTrue(provider.GetValue("TakeRows").AttemptedValue.IsEqual("5"));

            Assert.IsTrue(provider.ContainsPrefix("OrderRows"));
            Assert.IsTrue(provider.GetValue("OrderRows").AttemptedValue.IsEqual("Name desc"));
        }

        [TestMethod]
        public void ParseODataBlankString()
        {
            // given
            Http.IValueProvider provider;
            string queryString = "?$blbost=a";

            // create engine
            var engine = new ODataParser();
            ServiceMessage message = new ServiceMessage();
            provider = engine.Parse(queryString, ref message);

            IValueContainer container = provider.ToContainer();

            Assert.IsTrue(message.Messages.Count > 0);
            Assert.IsTrue(message.Messages[0].Description == "Not supported ODATA query option $blbost ");
        }

        [TestMethod]
        public void ParseODataTestTopQueryOption() //test $top (TakeRows) query option
        {
            // given
            Http.IValueProvider provider;
            string queryString = @"http://localhost:5555/Product/api/Employee/?$top=777";

            // create engine
            var engine = new ODataParser();
            ServiceMessage message = new ServiceMessage();
            provider = engine.Parse(queryString, ref message);

            IValueContainer container = provider.ToContainer();

            Assert.IsTrue(message.Messages.Count == 0);

            Assert.IsTrue(provider.GetValue("TakeRows").AttemptedValue.IsEqual("777"));
        }

        [TestMethod]
        public void ParseODataTestTopQueryOptionFail() //test $top (TakeRows) query option
        {
            // given
            Http.IValueProvider provider;
            string queryString = @"http://localhost:5555/Product/api/Employee/?$top=777 666 555";

            // create engine
            var engine = new ODataParser();
            ServiceMessage message = new ServiceMessage();
            provider = engine.Parse(queryString, ref message);

            IValueContainer container = provider.ToContainer();

            Assert.IsTrue(message.Messages.Count > 0);

            Assert.IsTrue(message.Messages[0].Description == "ODATA query option $top contains non-integer value");
        }

        [TestMethod]
        public void ParseODataTestTopMultipleUseQueryOptionFail() //test $top (TakeRows) query option
        {
            // given
            Http.IValueProvider provider;
            string queryString = @"http://localhost:5555/Product/api/Employee/?$top=777&$top=666";

            // create engine
            var engine = new ODataParser();
            ServiceMessage message = new ServiceMessage();
            provider = engine.Parse(queryString, ref message);

            IValueContainer container = provider.ToContainer();

            Assert.IsTrue(message.Messages.Count > 0);

            Assert.IsTrue(message.Messages[0].Description == "ODATA query option $top was specified more than once");
        }

        [TestMethod]
        public void ParseODataTestSkipQueryOption() //test $skip (SkipRows) query option
        {
            // given
            Http.IValueProvider provider;
            string queryString = @"http://localhost:5555/Product/api/Employee/?$skip=777";

            // create engine
            var engine = new ODataParser();
            ServiceMessage message = new ServiceMessage();
            provider = engine.Parse(queryString, ref message);

            IValueContainer container = provider.ToContainer();

            Assert.IsTrue(message.Messages.Count == 0);

            Assert.IsTrue(provider.GetValue("SkipRows").AttemptedValue.IsEqual("777"));
        }

        [TestMethod]
        public void ParseODataTestSkipQueryOptionFail() //test $skip (SkipRows) query option
        {
            // given
            Http.IValueProvider provider;
            string queryString = @"http://localhost:5555/Product/api/Employee/?$skip=777 666 555";

            // create engine
            var engine = new ODataParser();
            ServiceMessage message = new ServiceMessage();
            provider = engine.Parse(queryString, ref message);

            IValueContainer container = provider.ToContainer();

            Assert.IsTrue(message.Messages.Count > 0);

            Assert.IsTrue(message.Messages[0].Description == "ODATA query option $skip contains non-integer value");
        }

        [TestMethod]
        public void ParseODataTestSkipMultipleUseQueryOptionFail() //test $skip (SkipRows) query option
        {
            // given
            Http.IValueProvider provider;
            string queryString = @"http://localhost:5555/Product/api/Employee/?$skip=777&$skip=666";

            // create engine
            var engine = new ODataParser();
            ServiceMessage message = new ServiceMessage();
            provider = engine.Parse(queryString, ref message);

            IValueContainer container = provider.ToContainer();

            Assert.IsTrue(message.Messages.Count > 0);

            Assert.IsTrue(message.Messages[0].Description == "ODATA query option $skip was specified more than once");
        }

        [TestMethod]
        public void ParseODataTestOrderBy() //test $orderby (OrderRows) query option
        {
            // given
            Http.IValueProvider provider;
            string queryString = @"http://localhost:5555/Product/api/Employee/?$orderby=Name asc, Surname desc, BirthDate, Age asc";

            // create engine
            var engine = new ODataParser();
            ServiceMessage message = new ServiceMessage();
            provider = engine.Parse(queryString, ref message);

            IValueContainer container = provider.ToContainer();

            Assert.IsTrue(message.Messages.Count == 0);
            var result = provider.GetValue("OrderRows").AttemptedValue;

            Assert.IsTrue(provider.GetValue("OrderRows").AttemptedValue.IsEqual("Name asc,Surname desc,BirthDate asc,Age asc"));
        }


        [TestMethod]
        public void ParseODataTestOrderByFail() //test $orderby (OrderRows) query option
        {
            // given
            Http.IValueProvider provider;
            string queryString = @"http://localhost:5555/Product/api/Employee/?$orderby=";

            // create engine
            var engine = new ODataParser();
            ServiceMessage message = new ServiceMessage();
            provider = engine.Parse(queryString, ref message);

            IValueContainer container = provider.ToContainer();

            Assert.IsTrue(message.Messages.Count > 0);

            Assert.IsTrue(message.Messages[0].Description == "Wrong format of the ODATA string. Incorrect syntax after $orderby");
        }

        [TestMethod]
        public void ParseODataTestOrderByFail2() //test $orderby (OrderRows) query option
        {
            // given
            Http.IValueProvider provider;
            string queryString = @"http://localhost:5555/Product/api/Employee/?$orderby=name,,,,as";

            // create engine
            var engine = new ODataParser();
            ServiceMessage message = new ServiceMessage();
            provider = engine.Parse(queryString, ref message);

            IValueContainer container = provider.ToContainer();

            Assert.IsTrue(message.Messages.Count > 0);

            Assert.IsTrue(message.Messages[0].Description == "Wrong format of the ODATA string. Incorrect syntax after $orderby");
        }

        [TestMethod]
        public void ParseODataTestSelect() //test $select (SelectColumns) query option
        {
            // given
            Http.IValueProvider provider;
            string queryString = @"http://localhost:5555/Product/api/Employee/?$select=Name,Surname, BirthDate  ,  Age";

            // create engine
            var engine = new ODataParser();
            ServiceMessage message = new ServiceMessage();
            provider = engine.Parse(queryString, ref message);

            IValueContainer container = provider.ToContainer();

            Assert.IsTrue(message.Messages.Count == 0);

            Assert.IsTrue(provider.GetValue("SelectColumns").AttemptedValue.IsEqual("Name,Surname,BirthDate,Age"));
        }

        [TestMethod]
        public void ParseODataTestSelectFail() //test $orderby (OrderRows) query option
        {
            // given
            Http.IValueProvider provider;
            string queryString = @"http://localhost:5555/Product/api/Employee/?$select=";

            // create engine
            var engine = new ODataParser();
            ServiceMessage message = new ServiceMessage();
            provider = engine.Parse(queryString, ref message);

            IValueContainer container = provider.ToContainer();

            Assert.IsTrue(message.Messages.Count > 0);

            Assert.IsTrue(message.Messages[0].Description == "Wrong format of the ODATA string. Incorrect syntax after $select");
        }

        [TestMethod]
        public void ParseODataTestSelectFail2() //test $orderby (OrderRows) query option
        {
            // given
            Http.IValueProvider provider;
            string queryString = @"http://localhost:5555/Product/api/Employee/?$select=name,,,,as";

            // create engine
            var engine = new ODataParser();
            ServiceMessage message = new ServiceMessage();
            provider = engine.Parse(queryString, ref message);

            IValueContainer container = provider.ToContainer();

            Assert.IsTrue(message.Messages.Count > 0);

            Assert.IsTrue(message.Messages[0].Description == "Wrong format of the ODATA string. Incorrect syntax after $select");
        }

        #region Additional test attributes
        [TestInitialize]
        public override void MyTestInitialize()
        {
            base.MyTestInitialize(); // Use TestInitialize to run code before running each test
        }

        [TestCleanup]
        public override void MyTestCleanup()
        {
            base.MyTestCleanup(); // Use TestCleanup to run code after each test has run
        }
        #endregion
    }
}

